package com.csii.autocode.service.impl;

import cn.hutool.core.util.StrUtil;
import com.csii.autocode.config.ReadConfig;
import com.csii.autocode.config.ReadTemplate;
import com.csii.autocode.service.CommonCodeGenerate;
import com.csii.autocode.utils.NonceUtils;
import com.csii.autocode.entity.ColumnVo;
import com.csii.autocode.entity.MainTableVo;
import com.csii.autocode.entity.SubTableVo;
import com.csii.autocode.service.IGenerate;
import com.csii.autocode.utils.DbReadTableUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CodeGenerateOneToMany extends CommonCodeGenerate implements IGenerate {
    private static final Logger logger = LoggerFactory.getLogger(CodeGenerateOneToMany.class);

    private static String f;

    public static final String a = "A";

    public static final String b = "B";

    private MainTableVo mainTableVo;

    private List<ColumnVo> columnVos;

    private List<ColumnVo> columnVoList;

    private List<SubTableVo> subTableVos;

    private static DbReadTableUtil dbReadTableUtil = new DbReadTableUtil();

    public CodeGenerateOneToMany(MainTableVo mainTableVo, List<SubTableVo> subTables) {
        this.subTableVos = subTables;
        this.mainTableVo = mainTableVo;
    }

    public CodeGenerateOneToMany(MainTableVo mainTableVo, List<ColumnVo> mainColums, List<ColumnVo> originalMainColumns, List<SubTableVo> subTables) {
        this.mainTableVo = mainTableVo;
        this.columnVos = mainColums;
        this.columnVoList = originalMainColumns;
        this.subTableVos = subTables;
    }

    public Map<String, Object> getCreateInfo() throws Exception {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("bussiPackage", ReadConfig.getBassiPackage());
        hashMap.put("entityPackage", this.mainTableVo.getEntityPackage());
        hashMap.put("entityName", this.mainTableVo.getEntityName());
        hashMap.put("tableName", this.mainTableVo.getTableName());
        hashMap.put("ftl_description", this.mainTableVo.getFtlDescription());
        hashMap.put("primaryKeyField", ReadConfig.getDbTableId());
        if (this.mainTableVo.getFieldRequiredNum() == null)
            this.mainTableVo.setFieldRequiredNum(Integer.valueOf(StrUtil.isNotEmpty(ReadConfig.getField_required_num()) ? Integer.parseInt(ReadConfig.getField_required_num()) : -1));
        if (this.mainTableVo.getSearchFieldNum() == null)
            this.mainTableVo.setSearchFieldNum(Integer.valueOf(StrUtil.isNotEmpty(ReadConfig.getPageSearchFiledNum()) ? Integer.parseInt(ReadConfig.getPageSearchFiledNum()) : -1));
        if (this.mainTableVo.getFieldRowNum() == null)
            this.mainTableVo.setFieldRowNum(Integer.valueOf(Integer.parseInt(ReadConfig.getField_row_num())));
        hashMap.put("tableVo", this.mainTableVo);
        try {
            if (this.columnVos == null || this.columnVos.size() == 0)
                this.columnVos = DbReadTableUtil.getTableColumn(this.mainTableVo.getTableName());
            if (this.columnVoList == null || this.columnVoList.size() == 0)
                this.columnVoList = DbReadTableUtil.getVolumnVoList(this.mainTableVo.getTableName());
            hashMap.put("columns", this.columnVos);
            hashMap.put("originalColumns", this.columnVoList);
            for (ColumnVo columnVo : this.columnVoList) {
                if (columnVo.getFieldName().toLowerCase().equals(ReadConfig.getDbTableId().toLowerCase()))
                    hashMap.put("primaryKeyPolicy", columnVo.getFieldType());
            }
            for (SubTableVo subTableVo : this.subTableVos) {
                if (subTableVo.getColums() == null || subTableVo.getColums().size() == 0) {
                    List list = DbReadTableUtil.getTableColumn(subTableVo.getTableName());
                    subTableVo.setColums(list);
                }
                if (subTableVo.getOriginalColumns() == null || subTableVo.getOriginalColumns().size() == 0) {
                    List list = DbReadTableUtil.getVolumnVoList(subTableVo.getTableName());
                    subTableVo.setOriginalColumns(list);
                }
                String[] arrayOfString = subTableVo.getForeignKeys();
                ArrayList<String> arrayList = new ArrayList();
                for (String str : arrayOfString)
                    arrayList.add(DbReadTableUtil.toUpperCase(str));
                subTableVo.setForeignKeys(arrayList.<String>toArray(new String[0]));
                subTableVo.setOriginalForeignKeys(arrayOfString);
            }
            hashMap.put("subTables", this.subTableVos);
        } catch (Exception exception) {
            throw exception;
        }
        long l = NonceUtils.randomLong() + NonceUtils.currentMills();
        hashMap.put("serialVersionUID", String.valueOf(l));
        logger.info("code template data: " + hashMap.toString());
        return hashMap;
    }

    public List<String> generateCodeFile(String stylePath) throws Exception {
        String str1 = ReadConfig.getProjectPath();
        Map<String, Object> map = getCreateInfo();
        String str2 = ReadConfig.template_path;
        if (stringCut(str2, "/").equals("auto-code/code-template")) {
            str2 = "/" + stringCut(str2, "/") + "/onetomany";
            ReadConfig.setTemplatePath(str2);
        }
        ReadTemplate template = new ReadTemplate(str2);
        template.setStylePath(stylePath);
        createAllFiles(template, str1, map);
        logger.info("----- csii-boot ---- generate  code  success =======> 主表名：" + this.mainTableVo.getTableName());
        return this.outStrs;
    }

    public List<String> generateCodeFile(String projectPath, String templatePath, String stylePath) throws Exception {
        if (projectPath != null && !"".equals(projectPath))
            ReadConfig.setProjectPath(projectPath);
        if (templatePath != null && !"".equals(templatePath))
            ReadConfig.setTemplatePath(templatePath);
        generateCodeFile(stylePath);
        return this.outStrs;
    }
}
